from app import db
from app.mappers.models import DepartmentInfo


class DepartmentInfoDao():
    def __init__(self):
        print("DepartmentInfoDao is running")  # 类初始化时输出提示信息

    def findAll(self):
        try:
            departmentInfo = DepartmentInfo.query.all()
            return departmentInfo
        except Exception as e:
            print(e)
            pass

    def addOne(self, departmentInfo):
        flag = True
        try:
            db.session.add(departmentInfo)
            db.session.commit()
        except Exception as e:
            flag = False
            db.session.rollback()
            pass
        return flag

    def deleteById(self, id):
        flag = True
        try:
            departmentInfo = DepartmentInfo.query.get(id)
            if departmentInfo is not None:
                db.session.delete(departmentInfo)
                db.session.commit()
            else:
                flag = False
        except Exception as e:
            print(e)
            flag = False
            db.session.rollback()
        return flag

    def updateById(self, id, name):
        flag = True
        try:
            departmentInfo = DepartmentInfo.query.get(id)
            departmentInfo.id = id
            departmentInfo.name = name
            db.session.commit()  # 提交事务
        except Exception as e:
            print(e)
            flag = False
            db.session.rollback()
        return flag
